package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;

    @RequestMapping("login")
    public String login(String userName, String password, HttpServletRequest request){
        UserBean userBean = userService.login(userName, password);
        if (userBean!=null){
            request.getSession().setAttribute("loginUser",userBean);
            return "ok";
        }return "no";
    }
    @RequestMapping("add")
    public String add(String userName,String password,String imgPath,String email){
        UserBean userBean = new UserBean(userName,password,email,imgPath);
        userService.add(userBean);
        return "ok";
    }
    @RequestMapping("findByName")
    public String findByName(String userName){
        UserBean userBean = userService.findByName(userName);
        if (userBean == null){
            return "ok";
        }return "no";
    }
    @RequestMapping("getLoginUser")
    public UserBean getLoginUser(HttpServletRequest request){
        return (UserBean) request.getSession().getAttribute("loginUser");
    }
}
